/*
 * Team Java
 * Michael Pogson, Michael Satranjr, Stephen Hunter
 * Project 1
 * 4/26/12
 */
package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controller.Manager;

/**
 * Frame allows the user to input data for running the web crawler.
 * @author Michael Pogson, Michael Satran Jr, Stephen Hunter
 * @version Spring 2012
 */
public class MainFrame {

	private final JFrame my_frame;
	
	private final JPanel my_panel;
	
	private final JPanel my_mid_panel;
	
	private final JPanel my_south_panel;
	
	private final JPanel my_thread_panel;
	
	private final JLabel my_keywords_label;
	
	private final JLabel my_selection_label;
	
	private final JLabel my_url_label;
	
	private final JLabel my_max_url_label;
	
	private final JLabel my_thread_label;
	
	private final JTextField my_keywords_field;
	
	private final JTextField my_url_field;
	
	private final JTextField my_max_url_field;
	
	private final JTextField my_thread_field;
	
	private final JButton my_threaded_button;
	
	private final JButton my_unthreaded_button;
	
	private boolean my_isURLValid = true;
	
	/**
	 * Default number for max URLs.
	 */
	private int my_max_urls = 25;
	
	/**
	 * Default number of parsers.
	 */
	private int my_parsers = 5;
	
	/**
	 * Default number of retrievers.
	 */
	private int my_retrievers = 5;
	
	
	/**
	 * Constructor sets up all fields appropriately.
	 */
	public MainFrame() {
		my_frame = new JFrame("Web Spider");
		my_frame.setLayout(new BorderLayout());
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_frame.setLocationRelativeTo(null);
		my_frame.setVisible(true);
		
		my_panel = new JPanel(new BorderLayout());
		my_mid_panel = new JPanel(new BorderLayout());
		my_south_panel = new JPanel(new BorderLayout());
		my_thread_panel = new JPanel(new BorderLayout());
		
		my_threaded_button = new JButton("Threaded");
		my_unthreaded_button = new JButton("Unthreaded");
		
		my_keywords_label = new JLabel("Please enter the keywords you would like to search for" +
										" separated by a comma.");
		my_selection_label = new JLabel("Would you like to run the program as threaded or unthreaded?");
		my_url_label = new JLabel("Please enter a seed URL.");
		my_max_url_label = new JLabel("What is the maximum number of URLs you would like to retrieve?");
		my_thread_label = new JLabel("How many parsers and retrievers would you like to use?" + 
										" Separate by a comma.");
		
		my_keywords_field = new JTextField();
		my_url_field = new JTextField();
		my_max_url_field = new JTextField();
		my_thread_field = new JTextField();
	}
	
	/**
	 * Sets up the look of the frame.
	 */
	public void setUp() {
		setUpButtons();
		
		my_panel.add(my_keywords_label, BorderLayout.NORTH);
		my_panel.add(my_keywords_field, BorderLayout.CENTER);
		my_panel.add(my_url_label, BorderLayout.SOUTH);
		my_frame.add(my_panel, BorderLayout.NORTH);
		
		my_mid_panel.add(my_url_field, BorderLayout.NORTH);
		my_mid_panel.add(my_max_url_label, BorderLayout.CENTER);
		my_mid_panel.add(my_max_url_field, BorderLayout.SOUTH);
		my_frame.add(my_mid_panel, BorderLayout.CENTER);
		
		my_thread_panel.add(my_thread_label, BorderLayout.NORTH);
		my_thread_panel.add(my_thread_field, BorderLayout.CENTER);
		my_thread_panel.add(my_selection_label, BorderLayout.SOUTH);
		
		my_south_panel.add(my_thread_panel, BorderLayout.NORTH);
		my_south_panel.add(my_threaded_button, BorderLayout.EAST);
		my_south_panel.add(my_unthreaded_button, BorderLayout.WEST);
		my_frame.add(my_south_panel, BorderLayout.SOUTH);
		
		my_frame.pack();
	}
	
	/**
	 * Sets up the buttons with their actions.
	 */
	private void setUpButtons() {
		my_threaded_button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				Manager man = takeAction(); //Having manager field would require creating useless
				if(my_isURLValid) {			//temporary manager in constructor.
					man.setMaxURLs(my_max_urls);
					man.initiate();				
					man.startThreaded(my_retrievers, my_parsers);
				}
			}
		});
		
		my_unthreaded_button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				
				if(my_isURLValid) {
					Manager man = takeAction();
					man.setMaxURLs(my_max_urls);
					man.initiate();
					Thread manager = new Thread(man);
					manager.start();
					//man.startUnthreaded();
				}
			}
		});
	}
	
	/**
	 * Takes the actions required on a button press.
	 * 
	 * @return The manager object with the values entered by the user
	 */
	private Manager takeAction() {
		my_frame.dispose();
		
		List<String> keywords = new LinkedList<String>();
		String text = my_keywords_field.getText();
		Scanner scan = new Scanner(text);
		scan.useDelimiter(",");
		
		while(scan.hasNext()) {
			keywords.add(scan.next());
		}
		
		List<URL> url = new LinkedList<URL>();
		try {
			url.add(new URL(my_url_field.getText()));
		} catch(final MalformedURLException e) {
			//MalformedURLException
			my_isURLValid = false;
		}
		
		String num = my_max_url_field.getText();
		try {
			my_max_urls = Integer.parseInt(num);
		} catch(final NumberFormatException e) {
			my_isURLValid = false;	//Just uses the boolean to kill the program instead of
		}							//making another one for the same purpose.
		
		String threads = my_thread_field.getText();
		scan = new Scanner(threads);
		scan.useDelimiter(",");
		String p = "";
		String r = "";
		if(scan.hasNext()) {
			p = scan.next();
		}
		if(scan.hasNext()) {
			r = scan.next();
		}
		
		try {
			my_parsers = Integer.parseInt(p);
			my_retrievers = Integer.parseInt(r);
		} catch(final NumberFormatException e) {
			//do nothing
		}
		
		if(my_max_urls > 9000) {
			System.out.println("That's.....OVER 9000!!!!!");
		}
		if (my_max_urls > 10000) {
			System.out.println("I don't really want to do " + my_max_urls +  " right now.");
			System.out.println("Let's make it 25.");
			my_max_urls = 25;
		}
		
		return new Manager(url, keywords);
	}
}
